<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设备编号" prop="deviceCode">
              <el-input v-model="formData.deviceCode" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input v-model="formData.deviceName" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="设备类型" prop="deviceType">
              <el-select v-model="formData.deviceType" placeholder="请选择设备类型">
                <!-- 使用从父组件传递的deviceTypeOptions -->
                <el-option v-for="(item, index) in deviceTypeOptions" :key="index" :label="item.label" :value="Number(item.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备状态" prop="status">
              <el-radio-group v-model="formData.status">
                <el-radio :label="0">正常</el-radio>
                <el-radio :label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as DeviceApi from '@/api/lamp/device';

export default {
  name: "DeviceForm",
  components: {
  },
  // 添加props接收父组件传递的deviceTypeOptions
  props: {
    deviceTypeOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        deviceType: undefined,
        deviceCode: undefined,
        deviceName: undefined,
        sortNum: undefined,
        status: undefined,
      },
      // 表单校验
      formRules: {
        deviceType: [{ required: true, message: '设备类型不能为空', trigger: 'change' }],
        deviceCode: [{ required: true, message: '设备编号不能为空', trigger: 'blur' }],
        deviceName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '设备状态不能为空', trigger: 'blur' }],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await DeviceApi.getDevice(id);
          this.formData = res.data;
          this.dialogTitle = "修改设备";
        } finally {
          this.formLoading = false;
        }
      } else {
        this.dialogTitle = "新增设备";
      }
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
                  this.formLoading = true;
      try {
        const data = this.formData;
                  // 修改的提交
        if (data.id) {
          await DeviceApi.updateDevice(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await DeviceApi.createDevice(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
                      /** 表单重置 */
    reset() {
      this.formData = {
                            id: undefined,
                            deviceType: undefined,
                            deviceCode: undefined,
                            deviceName: undefined,
                            sortNum: undefined,
                            status: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
